package com.sun.app.service.app;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;


import com.sun.app.service.BaseService;
import com.sun.util.AppResult;
import com.sun.util.RangeUtil;
import com.sun.util.TimeUtil;

public class ActivityService extends BaseService{
	
	@Override
	public Integer add(HttpServletRequest request, String ip) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Integer delete(HttpServletRequest request, String ip) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Integer update(HttpServletRequest request, String ip) {
		// TODO Auto-generated method stub
		return null;
	}
	//关注活动
	private static String INSERT_ATTENTION ="INSERT INTO c_activity_attention(UID,ACTID,STATUS) values(?,?,?)";
	private static String CHECK_CARE="SELECT count(ATTENTIONID) from c_activity_attention WHERE UID=? AND ACTID=? AND STATUS=?";
	
	//查询是否数据库中已有信息，0-无，1-有
	public int checkAttend(String type,String UID,String actId){
		System.out.println(UID+"                      "+actId);
		if(type=="care"){
			int re=jt.queryForInt(CHECK_CARE,UID,actId,0);
			System.out.println("Cre----------->"+re);
			if(re==0){
				System.out.println("care无信息");
				return 0;
			}
			else{
				System.out.println("care有信息");
				return 1;
			}
		}
		else{
			int re=jt.queryForInt(CHECK_CARE,UID,actId,1);
			System.out.println("Are----------->"+re);
			if(re==0){
				System.out.println("无信息");
				return 0;
			}
			else{
				System.out.println("有信息");
				return 1;
			}
		}
	}
	//留一个消息推送接口
	public AppResult careActivity(HttpServletRequest request){
		AppResult r = new AppResult();
		String userId = (String) request.getSession().getAttribute("UID");
		String actId = (String) request.getAttribute("actId");
		System.out.println("CARE--------->UID------->"+userId);
		System.out.println("actId------------->"+actId);
		//若没有数据则插入
		if((checkAttend("care",userId,actId)==0)&&userId!=null){
			int success=jt.update(INSERT_ATTENTION,userId,actId,0);
			r.setStatus(200);
			r.setResult("success");
			r.setMsg("success");
		}
		//若有数据则返回
		else{
			r.setStatus(300);
			r.setMsg("error");
			r.setResult("error");
		}
		
		return r;
		
	}
	//响应活动
	public AppResult attendActivity(HttpServletRequest request){
		AppResult r = new AppResult();
		String userId = (String) request.getSession().getAttribute("UID");
		String actId = (String) request.getAttribute("actId");
		//若没有数据则插入
		if((checkAttend("attend",userId,actId)==0)&&userId!=null){
			int success=jt.update(INSERT_ATTENTION,userId,actId,1);
			r.setStatus(200);
			r.setResult("success");
			r.setMsg("success");
		}
		//若有数据则返回
		else{
			r.setStatus(300);
			r.setMsg("error");
			r.setResult("error");
		}
		
		return r;
		
	}
	
	
	
	
	
	
	private static String SELECT_IP = "select BEGINIP,ENDIP,IPADDNAME,TMOBILE from c_ip where (SELECT INET_ATON(?))>=(SELECT INET_ATON(BEGINIP)) AND (SELECT INET_ATON(?))<=(SELECT INET_ATON(ENDIP))";
	public String[] getArea(String ip){
		
		List<Map<String, Object>> queryForList = jt.queryForList(SELECT_IP,ip,ip);
		
		System.out.println(queryForList);
		if(!queryForList.isEmpty()){
			Map<String, Object> map = queryForList.get(0);
			String ipNameT = (String) map.get("IPADDNAME");
			String tmobile = (String) map.get("TMOBILE");
			return splitArea(ipNameT,tmobile);
		}
		return null;
	}
	//过滤区、市、州、县Util
	public String[] splitArea(String ipNameT,String tmobile){
		String area = "";
		int temp = -1;
		int tempT = -1;
		int flag=0;
		if((temp = ipNameT.indexOf("区"))!=-1){
			int[] temp2 = {ipNameT.indexOf("省"),ipNameT.indexOf("市"),ipNameT.indexOf("州"),ipNameT.indexOf("县")};
			Arrays.sort(temp2);
			for(int i=0;i<temp2.length;i++){
				if(temp2[i]==-1){
					flag++;
				}
			}
			int temp3 = temp2[5-flag];
			int temp4 = temp2[4-flag];
			area = ipNameT.substring(temp3+1);//获取第一次的区、截取后面部分
			String city = ipNameT.substring(temp4+1, (temp4+1)+area.length());
			String[] po = {city,area};
			return po;
		}else if((tempT = tmobile.indexOf("区"))!=-1){
			int[] temp2 = {ipNameT.indexOf("省"),tmobile.indexOf("市"),tmobile.indexOf("州"),tmobile.indexOf("县")};
			Arrays.sort(temp2);
			for(int i=0;i<temp2.length;i++){
				if(temp2[i]==-1){
					flag++;
				}
			}
			int temp3 = temp2[5-flag];
			int temp4 = temp2[4-flag];
			area = ipNameT.substring(temp3+1);//获取第一次的区、截取后面部分
			String city = ipNameT.substring(temp4+1, (temp4+1)+area.length());
			String[] po = {city,area};
			return po;
		}
		return null;
	}
	
	/*
	 * 是否用区域ID作为主键、个人认为地区可以用名字就OK
	 */
	private static final String SELECT_AREAID_BY_AREANAME = "select ar.AREAID,c.CODE from c_area ar,m_city c where ar.CITYID=c.CODE and ar.AREANAME=? and c.CITYNAME=?";
	public List<Map<String, Object>> getAreaIdByName(String areaName,String cityName){
		if(jt.queryForList(SELECT_AREAID_BY_AREANAME,areaName,cityName).get(0)==null){
			return new ArrayList<Map<String,Object>>();
		}
		return jt.queryForList(SELECT_AREAID_BY_AREANAME,areaName,cityName);
	}
	
	//第一次登陆时得到一个默认Activity
	private static final String SELECT_ACTIVITY = "select APPATH,u.UID,ACTRANGE,AREANAME,CITYNAME,TIPNAME,STREET,TITLE,STREETDETAIL,BEGINTIME,ENDTIME,DETAIL,PLAN,a.STATE,USERTRUENAME,AREANAME,a.ACTID from c_activity_photo ap,c_activity a,c_tip t,m_city c,c_area ar,c_user u " +
			"where ar.AREAID=a.AREAID and ap.ACTID=a.ACTID and t.TIPID=a.TIPID and u.UID = a.UID and c.CODE = ar.CITYID and c.CODE=? and ar.AREAID=? order by (select Count(*) from c_activity_attention where c_activity_attention.ACTID=a.ACTID) DESC limit 0,1";
	public Map<String,Object> getActivity(int CITYID,int areaId){
		//如果读不出IP地址、那么给其默认项、
		if(areaId==0){
			return null;
		}
		return jt.queryForMap(SELECT_ACTIVITY,CITYID,areaId);
	}
	
	//Ajax通过id获得活动
	private static final String SELECT_ACTIVITY_BY_PK = "select APPATH,u.UID,ACTRANGE,AREANAME,CITYNAME,TIPNAME,STREET,TITLE,STREETDETAIL,BEGINTIME,ENDTIME,DETAIL,PLAN,a.STATE,USERTRUENAME,AREANAME,a.ACTID from c_activity_photo ap,c_activity a,c_tip t,m_city c,c_area ar,c_user u " +
	"where ap.ACTID=a.ACTID and t.TIPID=a.TIPID and u.UID = a.UID and c.CODE = ar.CITYID and ar.AREAID=a.AREAID and a.ACTID=?";
	public AppResult getActivity(HttpServletRequest request){
	//如果读不出IP地址、那么给其默认项、
		int ACTID = Integer.valueOf(request.getParameter("ACTID"));
		AppResult r = new AppResult();
		if(ACTID==0){
			r.setStatus(300);
			return r;
		}
		Map<String, Object> activity = jt.queryForMap(SELECT_ACTIVITY_BY_PK,ACTID);
		Date BEGINTIME = (Date) activity.get("BEGINTIME");
		Date ENDTIME = (Date) activity.get("ENDTIME");
		String beginTime = TimeUtil.DateFormat(BEGINTIME);//利用工具类把时间输出为String类型
		String endTime = TimeUtil.DateFormat(ENDTIME);
		String range = (String) activity.get("ACTRANGE");
		Map<String,String> rangeMap = RangeUtil.RangetoPeople(range);
		List<Map<String, Object>> attend=getAttendList((Integer) activity.get("ACTID"));//获取参加人List
		List<Map<String, Object>> attendFriendList = getAttendFriendList((String)request.getSession().getAttribute("UID"), (Integer) activity.get("ACTID"));//获取参加好友List
		int friendCount = getCountOfFriend((String)request.getSession().getAttribute("UID"));
		System.out.println("attend:"+attend);
		System.out.println(attend.size());
		System.out.println("attendFriendList:"+attendFriendList);
		System.out.println(attendFriendList.size());
		activity.remove("ACTRANGE");
		activity.remove("BEGINTIME");
		activity.remove("ENDTIME");
		activity.put("BEGINTIME", beginTime);
		activity.put("ENDTIME", endTime);
		activity.put("rangeMap", rangeMap);
		activity.put("attend", attend);
		activity.put("attendCount", attend.size());
		activity.put("attendFriendList", attendFriendList);
		activity.put("attendFriendListCount", attendFriendList.size());
		activity.put("friendCount", friendCount);
		r.setResult(activity);
		r.setStatus(200);
		r.setMsg("success");
		return r;
	}
	
	
	//得到该学校TOP10
	private static final String GET_TOP10_USER = "select * from t_top10_user  where SCHOOLID= ? order by TOP";
	private static final String GET_TOP10_ACT = "select * from t_top10_act  where SCHOOLID= ? order by TOP";
	private static final String GET_TOP10_ROUND = "select * from t_top10_round  where SCHOOLID= ? order by TOP";
	private static final String GET_USER_DETAIL = "select cu.USERTRUENAME,cud.LIMG from c_user cu,c_user_hdphoto cud where cu.UID =cud.UID and cu.UID = ?"; 
	private static final String GET_ACT_DETAIL = "select TITLE,APPATH from c_activity ca,c_activity_photo cap where ca.ACTID=cap.ACTID and ca.ACTID=?"; 
	private static final String GET_ROUND_DETAIL = "select RPATH,ROUNDNAME from c_round cr,c_round_photo crp where cr.ROUNDID=crp.RID and cr.ROUNDID=?"; 
	public AppResult getTop10(HttpServletRequest request){
		AppResult r =new AppResult();
		String schoolId = request.getParameter("schoolId");
		List<Map<String, Object>> top10UserList = jt.queryForList(GET_TOP10_USER,schoolId);
		List<Map<String, Object>> top10ActList = jt.queryForList(GET_TOP10_ACT,schoolId);
		List<Map<String, Object>> top10RoundList = jt.queryForList(GET_TOP10_ROUND,schoolId);
		Iterator<Map<String,Object>> it = top10UserList.iterator();
		Iterator<Map<String,Object>> itt = top10ActList.iterator();
		Iterator<Map<String,Object>> ittt = top10RoundList.iterator();
		ArrayList userList = new ArrayList();
		ArrayList actList = new ArrayList();
		ArrayList roundList = new ArrayList();
		//得到top10用户榜单
		while(it.hasNext()){
			Map next = it.next();
			String UID = (String) next.get("UID");
			Map<String, Object> userDetailMap = jt.queryForMap(GET_USER_DETAIL,UID);
			String userName = (String) userDetailMap.get("USERTRUENAME");
			String userPhoto = (String) userDetailMap.get("LIMG");
			next.put("userName", userName);
			next.put("userPhoto", userPhoto);
			userList.add(next);
		}
		//得到top10活动榜单
		while(itt.hasNext()){
			Map next = it.next();
			String ACTID = (String) next.get("ACTID");
			Map<String, Object> actDetailMap = jt.queryForMap(GET_ACT_DETAIL,ACTID);
			String actTitle = (String) actDetailMap.get("TITLE");
			String actPhoto = (String) actDetailMap.get("APPATH");
			next.put("actTitle", actTitle);
			next.put("actPhoto", actPhoto);
			actList.add(next);
		}
		//得到top10圈子榜单
		while(ittt.hasNext()){
			Map next = it.next();
			String ROUNDID = (String) next.get("ROUNDID");
			Map<String, Object> roundDetailMap = jt.queryForMap(GET_ROUND_DETAIL,ROUNDID);
			String roundName = (String) roundDetailMap.get("ROUNDNAME");
			String roundPhoto = (String) roundDetailMap.get("RPATH");
			next.put("roundName", roundName);
			next.put("roundPhoto", roundPhoto);
			roundList.add(next);
		}
		Map m = new HashMap();
		m.put("userList", userList);
		m.put("userListSize", userList.size());
		
		m.put("actList", actList);
		m.put("actListSize", actList.size());
		
		m.put("roundList", roundList);
		m.put("roundListSize", roundList.size());
		
		System.out.println(m);
		
		r.setResult(m);
		r.setMsg("success");
		r.setStatus(200);
		System.out.println(r+"!!!!!!!!!!!!!!-----------");
		return r;
	}


	//查询地图所在点活动
//	private static final String GET_POINTACT = "select TITLE,c_activity.ACTID,APPATH from c_activity,c_activity_photo where c_activity_photo.ACTID=c_activity.ACTID and ACTMAPID=? order by (select Count(*) from c_activity_attention where c_activity_attention.ACTID=c_activity.ACTID) DESC";
	private static final String GET_POINTACT = "select TITLE,c_activity.ACTID,APPATH from c_activity,c_activity_photo where c_activity_photo.ACTID=c_activity.ACTID and CITYID=? and c_activity.ACTMAPID=? order by (select Count(*) from c_activity_attention where c_activity_attention.ACTID=c_activity.ACTID) DESC";
	private static final String GET_SPONTACT = "select TITLE,c_activity.ACTID,APPATH from c_activity,c_activity_photo where c_activity_photo.ACTID=c_activity.ACTID and SCHOOLID=? and c_activity.ACTMAPID=? order by (select Count(*) from c_activity_attention where c_activity_attention.ACTID=c_activity.ACTID) DESC";
	public AppResult getAreaActivity(HttpServletRequest request){
		AppResult result= new AppResult();
		String actPointIdStr=request.getParameter("ACTPOINTID");
		String cityId = request.getParameter("cityId");
		//查询学校
		if(cityId.length()<=5){
			if(actPointIdStr!=null){
				System.out.println("查询学校ACTIVITY");
				System.out.println(actPointIdStr);
				int actPointId=Integer.valueOf(actPointIdStr);
				Map<String, Object> activity;
				List<Map<String, Object>> activityList = jt.queryForList(GET_POINTACT,cityId,actPointId);
				System.out.println(activityList);
				int activityListCount=activityList.size();
				System.out.println("activityListCount"+activityListCount);
				System.out.println(activityList);
				int actId= (Integer) activityList.get(0).get("ACTID");
				activity=jt.queryForMap(SELECT_ACTIVITY_BY_PK,actId);
				System.out.println(activity);
				//调用取得活动
				Date BEGINTIME = (Date) activity.get("BEGINTIME");
				System.out.println("BEGINTIME"+BEGINTIME);
				Date ENDTIME = (Date) activity.get("ENDTIME");
				System.out.println("ENDTIME"+ENDTIME);
				String beginTime = TimeUtil.DateFormat(BEGINTIME);//利用工具类把时间输出为String类型
				String endTime = TimeUtil.DateFormat(ENDTIME);
				System.out.println("beginTime"+beginTime+"endTime"+endTime);
				String range = (String) activity.get("ACTRANGE");
				Map<String,String> rangeMap = RangeUtil.RangetoPeople(range);
				System.out.println("rangeMap"+rangeMap);
				List<Map<String, Object>> attend=getAttendList((Integer) activity.get("ACTID"));//获取参加人List
				System.out.println("attend"+attend);
				List<Map<String, Object>> attendFriendList = getAttendFriendList((String)request.getSession().getAttribute("UID"), (Integer) activity.get("ACTID"));//获取参加好友List
				System.out.println("attendFriendList"+attendFriendList);
				int friendCount = getCountOfFriend((String)request.getSession().getAttribute("UID"));
				System.out.println("friendCount"+friendCount);
				activity.remove("ACTRANGE");
				activity.remove("BEGINTIME");
				activity.remove("ENDTIME");
				activity.put("BEGINTIME", beginTime);
				activity.put("ENDTIME", endTime);
				activity.put("rangeMap", rangeMap);
				activity.put("attend", attend);
				activity.put("attendCount", attend.size());
				activity.put("attendFriendList", attendFriendList);
				activity.put("attendFriendListCount", attendFriendList.size());
				activity.put("friendCount", friendCount);
				//结束
				activity.put("ACTIVITYLIST", activityList);
				activity.put("ACTIVITYLISTCOUNT", activityListCount);
				System.out.println("rangge"+rangeMap);
				System.out.println("attend"+attend);
				result.setResult(activity);
				result.setStatus(200);
				result.setMsg("success");
			}
			else{
				System.out.println("eooooooo");
				result.setStatus(300);
				result.setResult("error");
			}
		}
		//查询城市
		else{
			if(actPointIdStr!=null){
				System.out.println("查询地区ACTIVITY");
				System.out.println(actPointIdStr);
				int actPointId=Integer.valueOf(actPointIdStr);
				Map<String, Object> activity;
				List<Map<String, Object>> activityList = jt.queryForList(GET_POINTACT,cityId,actPointId);
				System.out.println(activityList);
				int activityListCount=activityList.size();
				System.out.println("activityListCount"+activityListCount);
				System.out.println(activityList);
				int actId= (Integer) activityList.get(0).get("ACTID");
				activity=jt.queryForMap(SELECT_ACTIVITY_BY_PK,actId);
				System.out.println(activity);
				//调用取得活动
				Date BEGINTIME = (Date) activity.get("BEGINTIME");
				System.out.println("BEGINTIME"+BEGINTIME);
				Date ENDTIME = (Date) activity.get("ENDTIME");
				System.out.println("ENDTIME"+ENDTIME);
				String beginTime = TimeUtil.DateFormat(BEGINTIME);//利用工具类把时间输出为String类型
				String endTime = TimeUtil.DateFormat(ENDTIME);
				System.out.println("beginTime"+beginTime+"endTime"+endTime);
				String range = (String) activity.get("ACTRANGE");
				Map<String,String> rangeMap = RangeUtil.RangetoPeople(range);
				System.out.println("rangeMap"+rangeMap);
				List<Map<String, Object>> attend=getAttendList((Integer) activity.get("ACTID"));//获取参加人List
				System.out.println("attend"+attend);
				List<Map<String, Object>> attendFriendList = getAttendFriendList((String)request.getSession().getAttribute("UID"), (Integer) activity.get("ACTID"));//获取参加好友List
				System.out.println("attendFriendList"+attendFriendList);
				int friendCount = getCountOfFriend((String)request.getSession().getAttribute("UID"));
				System.out.println("friendCount"+friendCount);
				activity.remove("ACTRANGE");
				activity.remove("BEGINTIME");
				activity.remove("ENDTIME");
				activity.put("BEGINTIME", beginTime);
				activity.put("ENDTIME", endTime);
				activity.put("rangeMap", rangeMap);
				activity.put("attend", attend);
				activity.put("attendCount", attend.size());
				activity.put("attendFriendList", attendFriendList);
				activity.put("attendFriendListCount", attendFriendList.size());
				activity.put("friendCount", friendCount);
				//结束
				activity.put("ACTIVITYLIST", activityList);
				activity.put("ACTIVITYLISTCOUNT", activityListCount);
				System.out.println("rangge"+rangeMap);
				System.out.println("attend"+attend);
				result.setResult(activity);
				result.setStatus(200);
				result.setMsg("success");
			}
			else{
				System.out.println("eooooooo");
				result.setStatus(300);
				result.setResult("error");
			}
		}
		return result;
	}
	
	/* 语句重写、获得活动的ID 与 名字。
	 * 获取活动List、小图
	 */
	private static final String SELECT_ACTIVITY_SLIST = "select TITLE,c_activity.ACTID,APPATH from c_activity,c_activity_photo where c_activity_photo.ACTID=c_activity.ACTID and CITYID=? and AREAID=? order by (select Count(*) from c_activity_attention where c_activity_attention.ACTID=c_activity.ACTID) DESC";
	public List<Map<String,Object>> getActivityList(int CITYID,int areaId){
		if(areaId==0){
			return null;
		}
		return jt.queryForList(SELECT_ACTIVITY_SLIST,CITYID,areaId);
	}
	//查询参与人数
	private static final String SELECT_ATTEND_LIST="select cu.UID,SIMG from c_user cu,c_user_hdphoto cp,c_activity_attention caa "+
	"where caa.UID=cu.UID and cu.UID=cp.UID and caa.STATUS=1 and ACTID=?";
	public List<Map<String,Object>> getAttendList(int actId){
		return jt.queryForList(SELECT_ATTEND_LIST,actId);
	}
	
	//查询参与好友Lisr
	private static final String SELECT_FRIEND = "select distinct f.FRIENDID as UID,cp.SIMG from c_friend f,c_user_hdphoto cp,c_activity_attention caa where caa.UID=f.FRIENDID and cp.UID=f.FRIENDID and f.UID=? and f.state=1 and caa.status=1";
	private static final String SELECT_USERNAME = "select USERTRUENAME from c_user where UID=?";
	public List<Map<String,Object>> getAttendFriendList(String UID,int actId){
		//UID为空！未判断
		List<Map<String, Object>> friendList = jt.queryForList(SELECT_FRIEND,UID);
		Iterator<Map<String, Object>> it = friendList.iterator();
		System.out.println("\n\n\n\n\n"+friendList+"\n\n\n\n\n");
		while(it.hasNext()){
			Map<String, Object> next = it.next();
			String USERTRUENAME = (String) jt.queryForMap(SELECT_USERNAME,next.get("UID")).get("USERTRUENAME");
			next.put("USERTRUENAME", USERTRUENAME);
		}
		System.out.println(friendList);
		return friendList;
	}		
		
	
	private static final String COUNT_FRIEND = "select count(*) from c_friend where UID=? and state=1";
	private static final String GET_CITYNAME = "select CITYNAME from m_city where CODE = ?";
	public int getCountOfFriend(String UID){
		return jt.queryForInt(COUNT_FRIEND,UID);
	}
	
	public String getIp(HttpServletRequest request){
		String ip = request.getHeader("x-forwarded-for");
		if (StringUtils.isEmpty(ip) || "unknown".equalsIgnoreCase(ip)) {
			ip = request.getHeader("x-forward-for");
			if (StringUtils.isEmpty(ip) || "unknown".equalsIgnoreCase(ip)) {
				ip = request.getRemoteAddr();
			}
		}
		return ip;
	}
	
	public AppResult getAdviceActivity(HttpServletRequest request){
		AppResult r = new AppResult();
		//获取IP
		String ip = getIp(request);
		//以后替换此数据
		if(getArea("119.6.14.82")!=null){
			String[] po = getArea("119.6.14.82");
			try {
				Map<String, Object> areaIdByName = getAreaIdByName(po[1], po[0]).get(0);
				int areaId = (Integer) areaIdByName.get("AREAID");
				int cityId = Integer.valueOf((String) areaIdByName.get("CODE"));
				//得到右下角推荐活动
				List<Map<String, Object>> adviceActivityList = getAdviceActivityList(cityId+"");
				System.out.println("cityID--->"+cityId);
				Map<String, Object> activity1 = getActivity(cityId, areaId);
				System.out.println(activity1);
				int ACTID = (Integer)activity1.get("ACTID");
				System.out.println(ACTID);
				Map<String, Object> activity = jt.queryForMap(SELECT_ACTIVITY_BY_PK,ACTID);
				System.out.println(activity);
				Date BEGINTIME = (Date) activity.get("BEGINTIME");
				Date ENDTIME = (Date) activity.get("ENDTIME");
				String beginTime = TimeUtil.DateFormat(BEGINTIME);//利用工具类把时间输出为String类型
				String endTime = TimeUtil.DateFormat(ENDTIME);
				String range = (String) activity.get("ACTRANGE");
				Map<String,String> rangeMap = RangeUtil.RangetoPeople(range);
				List<Map<String, Object>> attend=getAttendList((Integer) activity.get("ACTID"));//获取参加人List
				List<Map<String, Object>> attendFriendList = getAttendFriendList((String)request.getSession().getAttribute("UID"), (Integer) activity.get("ACTID"));//获取参加好友List
				int friendCount = getCountOfFriend((String)request.getSession().getAttribute("UID"));
				List<Map<String, Object>> activityList = getActivityList(cityId, areaId);
				List<Map<String, Object>> cityNameList = jt.queryForList(GET_CITYNAME,cityId);
				if(cityNameList.size()!=0){
					String cityName = (String)cityNameList.get(0).get("CITYNAME");
					activity.put("CITYNAME", cityName);
				}
				activity.remove("ACTRANGE");
				activity.remove("BEGINTIME");
				activity.remove("ENDTIME");
				activity.put("CITYID",cityId);
				activity.put("BEGINTIME", beginTime);
				activity.put("ENDTIME", endTime);
				activity.put("rangeMap", rangeMap);
				activity.put("attend", attend);
				activity.put("attendCount", attend.size());
				activity.put("attendFriendList", attendFriendList);
				activity.put("attendFriendListCount", attendFriendList.size());
				activity.put("friendCount", friendCount);
				activity.put("activityList", activityList);
				activity.put("activityListSize", activityList.size());
				activity.put("adviceActivityList", adviceActivityList);
				activity.put("adviceActivityListSize", adviceActivityList.size());
				r.setResult(activity);
				r.setStatus(200);
				r.setMsg("success");
			} catch (IndexOutOfBoundsException e) {
				// TODO: handle exception
				e.printStackTrace();
			}
		}else{
			/*
			 * 对于未查询到地区的操作
			 */
			r.setStatus(300);
			return r;
		}
		return r;
		
	}
	
	/*
	 * 活动左列表
	 * 得到 该城市 推荐活动List,最多4条
	 */
	private static final String SELECT_ADVICE_ACTIVITY_LIST = "select a.TITLE,a.ACTID,ap.APPATH,t.TIPNAME,COUNT(aa.UID) as ATTENTCOUNT,BEGINTIME,STREET,USERTRUENAME from c_activity a,c_activity_photo ap,c_tip t,c_activity_attention aa,c_advice_activity ad,c_user u where u.UID=a.UID and ap.ACTID=a.ACTID and a.TIPID=t.TIPID and aa.ACTID=a.ACTID and ad.ACTID=a.ACTID and aa.status=1 and a.CITYID=? group by a.ACTID";
	public List<Map<String, Object>> getAdviceActivityList(String cityId){
		return jt.queryForList(SELECT_ADVICE_ACTIVITY_LIST,cityId);
	}
	/*
	 * 得到活动List
	 */
	private static final String SELECT_ACTIVITY_LIST = "select a.TITLE,a.ACTID,ap.APPATH,t.TIPNAME,COUNT(aa.UID) as ATTENTCOUNT from c_activity a,c_activity_photo ap,c_tip t,c_activity_attention aa,c_area ar where ap.ACTID=a.ACTID and a.TIPID=t.TIPID and aa.ACTID=a.ACTID and ar.AREAID=a.AREAID and aa.status=1 and ar.AREAID=? group by a.ACTID";
	public List<Map<String, Object>> getActivityList(int areaId){
		return jt.queryForList(SELECT_ACTIVITY_LIST,areaId);
	}
	/*
	 * 得到该城市的区域
	 */
	private static final String SELECT_AREA_BY_CITYID = "select AREAID,AREANAME from c_area where CITYID=?";
	public List<Map<String,Object>> areaListByCityId(String cityId){
		return jt.queryForList(SELECT_AREA_BY_CITYID,cityId);
	}
	/*
	 * 通过活动ID查出该活动的关注人数
	 */
	private static final String COUNT_CARE_ACTIVITY = "select COUNT(*) from c_activity_attention where ACTID=?";
	public Integer getAcitivityCareCount(int ACTID){
		return jt.queryForInt(COUNT_CARE_ACTIVITY,ACTID);
	}
	/*
	 * 通过Ajax方法获取活动列表页数据
	 */
	public AppResult activityList(HttpServletRequest request){
		AppResult r = new AppResult();
		Map<String,Object> m = new HashMap<String,Object>();
		String cityId = (String)request.getAttribute("CODE");
		//得到该城市推荐活动
		List<Map<String, Object>> adviceActivityList = getAdviceActivityList(cityId);
		//迭代活动计算关注人数,并格式化时间
		Iterator<Map<String, Object>> it1 = adviceActivityList.iterator();
		int i = 0;
		while(it1.hasNext()){
			Map<String, Object> next = it1.next();
			int ACTID = (Integer)next.get("ACTID");
			Date d = (Date)next.get("BEGINTIME");
			Integer acitivityCareCount = getAcitivityCareCount(ACTID);
			String dateFormat = TimeUtil.DateFormatForAdvice(d);
			next.put("BEGINTIME", dateFormat);
			next.put("CARECOUNT", acitivityCareCount);
			adviceActivityList.set(i, next);
			i++;
		}
		
		//得到区域List
		List<Map<String, Object>> areaListByCityId = areaListByCityId(cityId);
		Iterator<Map<String, Object>> it = areaListByCityId.iterator();
		List<Map<String, Object>> tList = new ArrayList<Map<String,Object>>();
		while(it.hasNext()){
			Map<String, Object> next = it.next();
			int areaId = (Integer) next.get("AREAID");
			List<Map<String, Object>> activityList = getActivityList(areaId);
			Iterator<Map<String, Object>> iterator = activityList.iterator();
			i=0;
			while(iterator.hasNext()){
				Map<String, Object> next2 = iterator.next();
				int ACTID = (Integer)next2.get("ACTID");
				Integer acitivityCareCount = getAcitivityCareCount(ACTID);
				next2.put("CARECOUNT", acitivityCareCount);
				activityList.set(i, next2);
				i++;
			}
			next.put("activityList", activityList);
			next.put("activityListSize", activityList.size());
			tList.add(next);
			
		}
		m.put("activityList", tList);
		m.put("activityListSize", tList.size());
		m.put("adviceActivityList", adviceActivityList);
		m.put("adviceActivityListSize", adviceActivityList.size());
		System.out.println(m);
		r.setResult(m);
		r.setMsg("success");
		r.setStatus(200);
		return r;
	}
	
	//获得共同活动Id
	private static final String GET_COMMONACT_ID_PAGING = "select a.ACTID from (select ACTID from c_activity_attention where UID =? and STATUS=1) as a cross join (select ACTID from c_activity_attention where UID =? and STATUS=1) as b on a.ACTID=b.ACTID limit ?,?";
	private static final String GET_COMMONACT_ID_COUNT = "select count(a.ACTID) from (select ACTID from c_activity_attention where UID =? and STATUS=1) as a cross join (select ACTID from c_activity_attention where UID =? and STATUS=1) as b on a.ACTID=b.ACTID";
	//获取活动信息
	private static final String GET_ACTDETAIL_BY_ID="select PROVINCESNAME,APPATH,AREANAME,BEGINTIME,TITLE,STATE,STREET,ACTRANGE,CITYNAME from m_provinces mp,c_activity ca,m_city mc, c_area car,c_activity_photo cap where cap.ACTID=ca.ACTID and car.AREAID=ca.AREAID and mc.CODE=ca.CITYID and mp.CODE=mc.PROVINCESCODE and ca.ACTID=?";
	//获取活动响应人数
	private static final String GET_ACTATTEND="select count(UID) from c_activity_attention where ACTID=? and STATUS=0";
	public AppResult getCommonActivity(HttpServletRequest request){
		AppResult r = new AppResult();
		String UID = (String) request.getSession().getAttribute("UID");
		String friendId = request.getParameter("friendId");
		Integer PageCount=Integer.valueOf(request.getParameter("PageCount"));
		Integer Current_page = Integer.valueOf(request.getParameter("Current_page"));
		//从第几条
		int from=0+(Current_page-1)*PageCount;
		//到第几条
		int to = from+PageCount;
		//得到这一页的全部
		System.out.println("from---->"+from);
		System.out.println("to---->"+to);
		// 全部数量
		List<Map<String, Object>> commonActIdList = jt.queryForList(GET_COMMONACT_ID_PAGING,UID,friendId,from,to);
		Iterator<Map<String,Object>> it = commonActIdList.iterator();
		ArrayList commonActivityList = new ArrayList();
		while(it.hasNext()){
			Map next= it.next();
			String actId=next.get("ACTID")+"";
			
			Map<String, Object> activity=jt.queryForMap(GET_ACTDETAIL_BY_ID,actId);
			Date BEGINTIME = (Date) activity.get("BEGINTIME");
			String beginTime = TimeUtil.DateFormat(BEGINTIME);//利用工具类把时间输出为String类型
			String provinceName = (String) activity.get("PROVINCESNAME");
			String cityName = (String) activity.get("CITYNAME");
			String areaName = (String) activity.get("AREANAME");
			String streetName = (String) activity.get("STREET");
			String title = (String) activity.get("TITLE");
			String actRange = (String) activity.get("ACTRANGE");
			String state = activity.get("STATE")+"";
			String photo = (String) activity.get("APPATH");
			int attend = jt.queryForInt(GET_ACTATTEND,actId);
			next.put("beginTime", beginTime);
			next.put("provinceName", provinceName);
			next.put("cityName", cityName);
			next.put("areaName", areaName);
			next.put("streetName", streetName);
			next.put("title", title);
			next.put("actRange", actRange);
			next.put("state", state);
			next.put("attend", attend);
			next.put("photo", photo);
			commonActivityList.add(next);
		}
		int All_record = jt.queryForInt(GET_COMMONACT_ID_COUNT,UID,friendId);
		//全部页数
		int All_page=0;
		if(All_record%PageCount==0){
			All_page =All_record/PageCount ;
		}
		else{
			All_page =All_record/PageCount+1;
		}
		
		Map m = new HashMap();
		m.put("commonActivityList", commonActivityList);
		m.put("commonActivityListSize", commonActivityList.size());
		m.put("All_page", All_page);
		m.put("All_record", All_record);
		r.setResult(m);
		r.setMsg("success");
		r.setStatus(200);
		return r;
		
	}
	
	//查询用户参加的已结束的活动分页
	private static final String GET_ACTIVITY_PAGING_BY_ID = "select ca.ACTID,ca.TITLE,ca.ENDTIME from c_activity ca, c_activity_attention caa where ca.STATE=2 and caa.STATUS=1 and  caa.ACTID=ca.ACTID and caa.UID=? ";
	//查询用户参加的已结束的活动数目
	private static final String GET_ACTIVITY_COUNT_BY_ID = "select count(ca.ACTID) from c_activity ca, c_activity_attention caa where ca.STATE=2 and caa.STATUS=1 and  caa.ACTID=ca.ACTID and caa.UID=? ";
	//获取活动参加人数
	private static final String GET_ACTATTEND_ACT="select count(UID) from c_activity_attention where ACTID=? and STATUS=1";
	public AppResult getPersonActivityAttend(HttpServletRequest request){
		AppResult r = new AppResult();
		String UID = (String) request.getSession().getAttribute("UID");
		Integer PageCount=Integer.valueOf(request.getParameter("PageCount"));
		Integer Current_page = Integer.valueOf(request.getParameter("Current_page"));
		//从第几条
		int from=0+(Current_page-1)*PageCount;
		//到第几条
		int to = from+PageCount;
		//得到这一页的全部
		System.out.println("from---->"+from);
		System.out.println("to---->"+to);
		// 全部数量
		List<Map<String, Object>> personActivityList = jt.queryForList(GET_ACTIVITY_PAGING_BY_ID,UID);
		Iterator<Map<String, Object>> it =personActivityList.iterator();
		ArrayList personActList = new ArrayList();
		while(it.hasNext()){
			Map next = it.next();
			String actId = ""+next.get("ACTID");
			String actName = (String)next.get("TITLE");
			Date EndTime=(Date) next.get("ENDTIME");
			String actEndTime = TimeUtil.dateToDatetimeDay(EndTime);//利用工具类把时间输出为yy-mm-dd
			String actEndTimeMin=TimeUtil.dateToDatetimeMin(EndTime);//利用工具类把时间输出为hh:mm
			String actEndTimeAll=TimeUtil.DateFormat(EndTime);//利用工具类把时间输出为整体
			int attention = jt.queryForInt(GET_ACTATTEND,actId);
			int attend= jt.queryForInt(GET_ACTATTEND_ACT,actId);
			next.put("actId", actId);
			next.put("actName", actName);
			next.put("actEndTime", actEndTime);
			next.put("actEndTimeMin", actEndTimeMin);
			next.put("actEndTimeAll", actEndTimeAll);
			next.put("attention", attention);
			next.put("attend", attend);
			personActList.add(next);
		}
		int All_record = jt.queryForInt(GET_ACTIVITY_COUNT_BY_ID,UID);
		//全部页数
		int All_page=0;
		if(All_record%PageCount==0){
			All_page =All_record/PageCount ;
		}
		else{
			All_page =All_record/PageCount+1;
		}
		
		Map m = new HashMap();
		m.put("personActList", personActList);
		m.put("personActListSize", personActList.size());
		m.put("All_page", All_page);
		m.put("All_record", All_record);
		r.setResult(m);
		r.setMsg("success");
		r.setStatus(200);
		return r;
	}
	
	//得到用户关注的活动
	private static final String GET_ACTIVITY_ATTENTION_PAGING_BY_ID = "select ca.ACTID,ca.TITLE,ca.ENDTIME from c_activity ca, c_activity_attention caa where ca.STATE=2 and caa.STATUS=0 and  caa.ACTID=ca.ACTID and caa.UID=? ";
	//查询用户参加的已结束的活动数目
	private static final String GET_ACTIVITY_ATTENTION_COUNT_BY_ID = "select count(ca.ACTID) from c_activity ca, c_activity_attention caa where ca.STATE=2 and caa.STATUS=0 and  caa.ACTID=ca.ACTID and caa.UID=? ";
	//得到用户关注的活动
	public AppResult getPersonActivityAttention(HttpServletRequest request){
		AppResult r =new AppResult();
		String UID = (String) request.getSession().getAttribute("UID");
		Integer PageCount=Integer.valueOf(request.getParameter("PageCount"));
		Integer Current_page = Integer.valueOf(request.getParameter("Current_page"));
		//从第几条
		int from=0+(Current_page-1)*PageCount;
		//到第几条
		int to = from+PageCount;
		//得到这一页的全部
		System.out.println("from---->"+from);
		System.out.println("to---->"+to);
		// 全部数量
		List<Map<String, Object>> personActivityAttentionList = jt.queryForList(GET_ACTIVITY_ATTENTION_PAGING_BY_ID,UID);
		Iterator<Map<String, Object>> it =personActivityAttentionList.iterator();
		ArrayList personActAttentionList = new ArrayList();
		while(it.hasNext()){
			Map next = it.next();
			String actId = ""+next.get("ACTID");
			String actName = (String)next.get("TITLE");
			Date EndTime=(Date) next.get("ENDTIME");
			String actEndTime = TimeUtil.dateToDatetimeDay(EndTime);//利用工具类把时间输出为yy-mm-dd
			String actEndTimeMin=TimeUtil.dateToDatetimeMin(EndTime);//利用工具类把时间输出为hh:mm
			String actEndTimeAll=TimeUtil.DateFormat(EndTime);//利用工具类把时间输出为整体
			int attention = jt.queryForInt(GET_ACTATTEND,actId);
			int attend= jt.queryForInt(GET_ACTATTEND_ACT,actId);
			next.put("actId", actId);
			next.put("actName", actName);
			next.put("actEndTime", actEndTime);
			next.put("actEndTimeMin", actEndTimeMin);
			next.put("actEndTimeAll", actEndTimeAll);
			next.put("attention", attention);
			next.put("attend", attend);
			personActAttentionList.add(next);
		}
		int All_record = jt.queryForInt(GET_ACTIVITY_ATTENTION_COUNT_BY_ID,UID);
		//全部页数
		int All_page=0;
		if(All_record%PageCount==0){
			All_page =All_record/PageCount ;
		}
		else{
			All_page =All_record/PageCount+1;
		}
		
		Map m = new HashMap();
		m.put("personActList", personActAttentionList);
		m.put("personActListSize", personActAttentionList.size());
		m.put("All_page", All_page);
		m.put("All_record", All_record);
		r.setResult(m);
		r.setMsg("success");
		r.setStatus(200);
		return r;
	}
	
	
	//查询用户参加的所有的活动分页
	private static final String GET_ACTIVITY_PAGING_BY_ID_ALL = "select ca.ACTID,ca.TITLE,ca.BEGINTIME from c_activity ca, c_activity_attention caa where  caa.STATUS=1 and  caa.ACTID=ca.ACTID and caa.UID=? ";
	//查询用户参加的所有的活动数目
	private static final String GET_ACTIVITY_COUNT_BY_ID_ALL = "select count(ca.ACTID) from c_activity ca, c_activity_attention caa where  caa.STATUS=1 and  caa.ACTID=ca.ACTID and caa.UID=? ";
	
	public AppResult getPersonActivityAttendAll(HttpServletRequest request){
		AppResult r = new AppResult();
		String UID = (String) request.getSession().getAttribute("UID");
		Integer PageCount=Integer.valueOf(request.getParameter("PageCount"));
		Integer Current_page = Integer.valueOf(request.getParameter("Current_page"));
		//从第几条
		int from=0+(Current_page-1)*PageCount;
		//到第几条
		int to = from+PageCount;
		//得到这一页的全部
		System.out.println("from---->"+from);
		System.out.println("to---->"+to);
		// 全部数量
		List<Map<String, Object>> personActivityList = jt.queryForList(GET_ACTIVITY_PAGING_BY_ID_ALL,UID);
		Iterator<Map<String, Object>> it =personActivityList.iterator();
		ArrayList personActList = new ArrayList();
		while(it.hasNext()){
			Map next = it.next();
			String actId = ""+next.get("ACTID");
			String actName = (String)next.get("TITLE");
			Date BEGINTIME=(Date) next.get("BEGINTIME");
			String actEndTime = TimeUtil.dateToDatetimeDay(BEGINTIME);//利用工具类把时间输出为yy-mm-dd
			String actEndTimeMin=TimeUtil.dateToDatetimeMin(BEGINTIME);//利用工具类把时间输出为hh:mm
			String actEndTimeAll=TimeUtil.DateFormat(BEGINTIME);//利用工具类把时间输出为整体
			int attention = jt.queryForInt(GET_ACTATTEND,actId);
			int attend= jt.queryForInt(GET_ACTATTEND_ACT,actId);
			next.put("actId", actId);
			next.put("actName", actName);
			next.put("actEndTime", actEndTime);
			next.put("actEndTimeMin", actEndTimeMin);
			next.put("actEndTimeAll", actEndTimeAll);
			next.put("attention", attention);
			next.put("attend", attend);
			personActList.add(next);
		}
		int All_record = jt.queryForInt(GET_ACTIVITY_COUNT_BY_ID_ALL,UID);
		//全部页数
		int All_page=0;
		if(All_record%PageCount==0){
			All_page =All_record/PageCount ;
		}
		else{
			All_page =All_record/PageCount+1;
		}
		
		Map m = new HashMap();
		m.put("personActList", personActList);
		m.put("personActListSize", personActList.size());
		m.put("All_page", All_page);
		m.put("All_record", All_record);
		r.setResult(m);
		r.setMsg("success");
		r.setStatus(200);
		return r;
	}
	
	//寻找活动按照分页显示
	private static final String SEARCH_ACT_BY_NAME="select ca.ACTID,ca.TITLE,ca.BEGINTIME,ca.PLAN from c_activity ca where TITLE like ? limit ?,?  ";
	private static final String SEARCH_ACT_COUNT_BY_NAME="select count(ACTID) from c_activity ca where TITLE like ?   ";
	public AppResult searchActivity(HttpServletRequest request){
		AppResult r = new AppResult();
		String actNameStr ="%"+request.getParameter("actName")+"%";
		Integer PageCount=Integer.valueOf(request.getParameter("PageCount"));
		Integer Current_page = Integer.valueOf(request.getParameter("Current_page"));
		//从第几条
		int from=0+(Current_page-1)*PageCount;
		//到第几条
		int to = from+PageCount;
		//得到这一页的全部
		System.out.println("from---->"+from);
		System.out.println("to---->"+to);
	
		List<Map<String, Object>> searchActIdList = jt.queryForList(SEARCH_ACT_BY_NAME,actNameStr,from,to);
		Iterator<Map<String, Object>> it =searchActIdList.iterator();
		ArrayList searchActList = new ArrayList();
		while(it.hasNext()){
			Map next = it.next();
			String actId = ""+next.get("ACTID");
			Map<String, Object> activity=jt.queryForMap(GET_ACTDETAIL_BY_ID,actId);
			Date BEGINTIME = (Date) activity.get("BEGINTIME");
			String beginTime = TimeUtil.DateFormat(BEGINTIME);//利用工具类把时间输出为String类型
			String plan = ""+next.get("PLAN");
			String provinceName = (String) activity.get("PROVINCESNAME");
			String cityName = (String) activity.get("CITYNAME");
			String areaName = (String) activity.get("AREANAME");
			String streetName = (String) activity.get("STREET");
			String title = (String) activity.get("TITLE");
			String actRange = (String) activity.get("ACTRANGE");
			String state = activity.get("STATE")+"";
			String photo = (String) activity.get("APPATH");
			int attend = jt.queryForInt(GET_ACTATTEND,actId);
			next.put("beginTime", beginTime);
			next.put("provinceName", provinceName);
			next.put("cityName", cityName);
			next.put("areaName", areaName);
			next.put("streetName", streetName);
			next.put("title", title);
			next.put("actRange", actRange);
			next.put("state", state);
			next.put("attend", attend);
			next.put("photo", photo);
			next.put("plan", plan);
			searchActList.add(next);
		}
		
		// 全部数量
		int All_record = jt.queryForInt(SEARCH_ACT_COUNT_BY_NAME,actNameStr);
		//全部页数
		int All_page=0;
		if(All_record%PageCount==0){
			All_page =All_record/PageCount ;
		}
		else{
			All_page =All_record/PageCount+1;
		}
		Map m = new HashMap();
		m.put("searchActList",searchActList);
		m.put("searchActListSize",searchActList.size());
		m.put("All_page", All_page);
		m.put("All_record", All_record);
		r.setResult(m);
		r.setMsg("success");
		r.setStatus(200);
		return r;
	}
}
